﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Text;
using System.Text.RegularExpressions;

namespace Titan.Validation
{
    public static class ValidationUtils
    {
        public const string EmailAddressRegex = @"^([a-zA-Z0-9_'+*$%\^&!\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9:]{2,4})+$";
        public const string CurrencyRegex = @"(^\$?(?!0,?\d)\d{1,3}(,?\d{3})*(\.\d\d)?)$";
        public const string DateRegex =
            @"^(((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}|\d))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00|[048])))$";
        public const string NumericRegex = @"\d*";

        public const string FloatRegex = @"^([+]?)\\d*\\.\\d+$";

        /// <summary>
        /// Validates an argument is not null or empty.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="parameterName">Name of the parameter.</param>
        public static void ArgumentNotNullOrEmpty(string value, string parameterName)
        {
            if (value == null)
                throw new ArgumentNullException(parameterName);

            if (value.Length == 0)
                throw new ArgumentException(string.Format("'{0}' cannot be empty.", parameterName), parameterName);
        }

        /// <summary>
        /// 正float的验证
        /// </summary>
        /// <param name="value">验证的数据</param>
        /// <param name="failureMessage">错误描述信息</param>
        public static void ArgumentIsFloat(string value, string failureMessage)
        {
            ArgumentNotNullOrEmpty(value, failureMessage);

            if (!Regex.IsMatch(value, FloatRegex))
            {
                throw new ArgumentException(string.Format("'{0}' cannot be float.", failureMessage), failureMessage);
            }
        }

        /// <summary>
        /// 验证数据是否匹配.
        /// </summary>
        /// <param name="value">验证的数据</param>
        /// <param name="regex">正则表达式</param>
        /// <param name="result">验证结果</param>
        /// <param name="failureMessage">错误描述信息</param>
        public static void ArgumentIsMatch(string value, string regex, string failureMessage)
        {
            ArgumentNotNullOrEmpty(value, failureMessage);
            ArgumentNotNullOrEmpty(regex, "Regex string cannot be empty.");

            if (!Regex.IsMatch(value, regex))
            {
                throw new ArgumentException(string.Format("'{0}' cannot be Match.", failureMessage), failureMessage);
            }
        }

        
        /// <summary>
        /// Validates a type argument is an enum.
        /// </summary>
        /// <param name="type">The type argument.</param>
        /// <param name="failureMessage">Name of the parameter.</param>
        public static void ArgumentTypeIsEnum(Type type, string failureMessage)
        {
            ArgumentNotNull(type, "type");

            if (!type.IsEnum)
                throw new ArgumentException(string.Format("Type {0} is not an Enum.", type), failureMessage);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="failureMessage"></param>
        public static void ArgumentNotNull(object value, string failureMessage)
        {
            if (value == null)
                throw new ArgumentNullException(failureMessage);
        }

        /*
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="result"></param>
        public static void ArgumentIsNull(object value, ref bool result)
        {
            if (value == null)
            {
                result = true;
            }

            if (value is INullable && ((INullable)value).IsNull)
            {
                result = true;
            }

            if (value == DBNull.Value)
            {
                result = true;
            }
            result = false;
        }

        */
    }
}
